var DEBUG = false;
var ALERT_USR = false;

var trace = ( DEBUG && typeof console != "undefined" ) ? console.log : function() {}; 


var SPLIT_W = 12;
var SPLIT_H = 15;
var CARD_W = 71;
var CARD_H = 96;

//suit
var Club=0, Heart=1, Diamond=2, Spade=3; 
//area type
var FREE=0, HOME=1, FIELD=2, IDLE=3;
var ACE=0;
//move type
var NORMAL_MOVE=0, AUTO_PLAY=1, SUPER_MOVE=2, UNDO=3; 
var SUPER_MOVE_BEGIN=4, SUPER_MOVE_END=5;

Array.prototype.empty = function() {
    return this.length == 0;
}

Array.prototype.last = function() {
    return (this.length == 0) ? null : this[this.length-1];
}

Array.prototype.first = function() {
    return (this.length == 0) ? null : this[0];
}

Array.prototype.remove_all = function() {
    this.length = 0;
}

function alert_usr(msg) {
    if( ALERT_USR ) {
	if(!msg) 
	    msg = "You can't move like this!";
	alert(msg);
    }
}

Card = function() {
    this.id = null;
    this.dom = null;
    this.pos_x = null;
    this.pos_y = null;
    this.area = null;

    this.suit = function() {
	return (this.id-1) % 4;
    }
    this.value = function() {
	return Math.round((this.id-1)/4-0.5);
    }
    this.is_black = function() {
	return this.suit() == Club || this.suit() == Spade;
    }
    this.is_red = function() {
	return this.suit() == Diamond || this.suit() == Heart;
    }
    this.fit_home = function(dst) {
	if(dst == null) {
	    return this.value() == ACE;
	}
	return this.suit() == dst.suit() && this.value() == dst.value()+1;
    }
    this.fit_field = function(dst) {
	if(dst == null ) {
	    return true;
	}
	return this.value() == dst.value()-1 && (this.is_black() ^ dst.is_black());
    }
    this.highlight = function() {
	card_fn = "/site_media/card_" + this.id + "_hl.gif";
	this.dom.css('background-image', 'url('+card_fn+')');
    }
    this.normalize = function() {
	var card_fn ="/site_media/card_" + this.id + ".gif"; 
	this.dom.css('background-image', 'url('+card_fn+')');
    }
}

//for free 
FreeSlot = function() {
    this.card = null;
    this.dom = null;
    this.pos = null;

    this.empty = function() {
	return this.card == null;
    }
    this.set_card = function(card) {
	card.dom.hide();
	card.dom.css('margin-top', '0px');
	card.area = FREE;
	card.pos_x = this.pos;
	this.dom.empty();//remove matched flag
	this.dom.append(card.dom);
	this.card = card;
	card.dom.show();
    }
    this.set_empty = function() {
	this.dom.empty();
	this.card = null;
    }
};

//for home, like a stack
HomeSlot = function() {
    this.card = null;
    this.dom = null;
    this.pos = null;
    this.cards = new Array;

    this.empty = function() {
	return this.cards.empty();
    }
    this.set_empty = function() {
	this.dom.empty();
	this.cards.remove_all();
	this.card = null;
    }

    this.set_card = function(card) {
	if( !this.empty() ) {
	    this.dom.empty();//remove existed card and matched flag
	}
	card.dom.hide();
	card.dom.css('margin-top', '0px');
	card.area = HOME;
	card.pos_x = this.pos;
	this.dom.append(card.dom);
	this.card = card;
	card.dom.show();
	this.cards.push(card);
    }
    this.pop = function() {
	this.cards.pop();
	this.dom.empty();
	if(this.cards.empty()) {
	    this.card = null;
	} else {
	    this.card = this.cards.last();
	    this.dom.append(this.card.dom);
	}
    }
}

FieldColumn = function() {
    this.cards = new Array();
    this.dom = null;
    this.pos = null;
    this.push = function(card) {
	if(!this.empty()) {
	    this.last().dom.empty();//remove matched flag
	}
	card.area = FIELD;
	card.pos_x = this.pos;
	card.pos_y = this.cards.length;
	var margin_top = (card.pos_y == 0) ? 0 : -(CARD_H - SPLIT_H);
	card.dom.hide();
	card.dom.css('margin-top', margin_top+'px');
	this.dom.append(card.dom);
	card.dom.show();
	this.cards.push(card);
    }
    this.set_empty = function() {
	if( !this.cards.empty() ) {
	    this.cards.remove_all();
	    this.dom.empty();
	}
    }
    this.pop = function() {
	this.cards.pop();
    }
    this.empty = function() {
	return this.cards.empty();
    }
    this.last = function() {
	return this.cards.last();
    }
};

DeckCtrl = function() {
    this.dom_play_area = $('#play_area');
    this.dom_free_home = $('#free_home_area');
    this.dom_field = $('#field_area');
    this.pos_map = new Array(52+4+4);//field+free+home
    this.free_slots = new Array(4);
    this.home_slots = new Array(4);
    this.field_cols = new Array(8);
    this.sel_card = null;
    this.notify_left_card = null;
    this.card_count = 52;
    this.playing = false;
    this.steps = new Array();
    this.matched = new Array();

    function constructor() {
	trace('DeckCtrl::constructor');
    }

    this.init = function() {
	trace('DeckCtrl::init');
	var i;
	for(i=0;i<4;i++) {
	    var slot = new FreeSlot;
	    slot.pos = i;
	    slot.dom = $('<div class="freecell" id="' + i + '"></div>');
	    slot.dom.appendTo(this.dom_free_home);
	    this.free_slots[i] = slot;
	}
	this.dom_free_home.append( $('<div id="flag_area"></div>') );
	for(i=0;i<4;i++) {
	    var slot = new HomeSlot;
	    slot.pos = i;
	    slot.dom = $('<div class="homecell" id="' + i + '"></div>');
	    slot.dom.appendTo(this.dom_free_home);
	    this.home_slots[i] = slot;
	}
	for(i=0;i<8;i++) {
	    var field_col = new FieldColumn;
	    var margin_left = (i == 0) ? 0 : SPLIT_W;
	    field_col.dom = $('<div class="fieldcolumn" id="'+ i +'"></div>').css('margin-left', margin_left+'px');
	    field_col.pos = i;
	    field_col.dom.appendTo(this.dom_field);
	    this.field_cols[i] = field_col;
	}
    }

    this.do_test = function(dst) { 
	var src = this.sel_card.area == FREE ? this.free_slots[this.sel_card.pos_x] : this.field_cols[this.sel_card.pos_x];
	var test = false;
	if(src instanceof FreeSlot && dst instanceof FieldColumn) test = this.free2field(src, dst, null, true);
	if(src instanceof FieldColumn && dst instanceof FieldColumn) test = this.super_move(src, dst, true);
	if(dst instanceof HomeSlot) test = this.move2home(src, dst, null, true); 
	if(test) this.matched.push(dst);
    }

    this.test = function() {
	if( this.sel_card != null ) {
	    //NOT test free, can do it when move hover
	    //test field
	    for( var i = 0; i < 8 ; i++ ) {
		if(this.sel_card.area == FIELD && this.sel_card.pos_x == i) continue;
		this.do_test(this.field_cols[i]);
	    }
	    //test home
	    for( var i = 0; i < 4 ; i++ ) 
		this.do_test(this.home_slots[i]);
	}
    }

    this.has_select = function() { return this.sel_card != null; }

    this.select = function(card) {
	if(this.has_select() ) {
	    this.sel_card.normalize()
	    if (this.sel_card == card ) {
		trace('select same card in free slot again');
		this.sel_card = null;
		return;
	    }
	}
	card.highlight();
	this.sel_card = card;
	this.test();//do test
    }

    this.empty_select = function() {
	if(this.sel_card != null ) {
	    this.sel_card.normalize();
	    this.sel_card = null;
	    this.matched.remove_all();
	}
    }

    this.get_super_move_max = function(free_slots, empty_cols) {
	m = free_slots.length;
	n = empty_cols.length;
        return (n+1)*(2*m+n)/2 + 1
    }

    this.get_empty_free_slot = function() {
	for( var i=0;i<4;i++ ) {
	    var slot = this.free_slots[i];
	    if(slot.empty()) return slot;
	}
	return null;
    }

    this.get_empty_free_slots = function() {
	var free_slots = new Array;
	for(var i=0; i<4; i++) 
	    if( this.free_slots[i].empty() ) free_slots.push(this.free_slots[i]);
	return free_slots;
    }
    this.get_empty_cols = function(dst) {
	var empty_cols = new Array;
	for( var i=0;i<8;i++ ) {
	    if( this.field_cols[i].empty() && i != dst.pos)
		empty_cols.push(this.field_cols[i]);
	}
	return empty_cols;
    }
    //used in normal move, super move, undo
    this.field2field = function(src, dst, move_type) {
	dst.push(src.last());
	src.pop();
	this.record_step(move_type, src, dst);
    }

    this.super_move_pack = function(empty_free, empty_field, src, end, stack) {
	var done = function() { return src.empty() || src.last() == end; };
	if(done()) return;
	//move cards to free slots first
	for(var i=0 ; i < empty_free.length ; i++) {
	    this.field2free(src, empty_free[i], SUPER_MOVE);
	    stack.unshift([src, empty_free[i]]); 
	    if(done()) return;
	}
	//move cards to empty fields from left to right
	for(var i = 0 ; i < empty_field.length ; i++ ) {
	    this.field2field(src, empty_field[i], SUPER_MOVE);
	    stack.unshift([src, empty_field[i]]);
	    if(done()) return;
	}
	//move cards from (n-2),(n-3)....,0 to n-1 field col
	for(var i = empty_field.length-2; i>=0 ; i--) {
	    this.field2field(empty_fields[i], empty_field.last(), SUPER_MOVE);
	    stack.unshift([empty_fields[i], empty_field.last()]);
	}
	//move card from free to field, from right to left
	for( var i = empty_free.length-1; i>=0 ; i--) {
	    this.free2field(empty_free[i], empty_field.last());
	    stack.unshift([empty_free[i], empty_field.last()]);
	} 
	empty_field.pop();
	//call me again
	this.super_move_pack(empty_free, empty_field, src, end, stack);
    }

    //only used in undo
    this.moveFromHome = function(src, dst) {
	if(dst instanceof FieldColumn) dst.push(src.card);
	if(dst instanceof FreeSlot) dst.set_card(src.card);
	src.pop();
	this.card_count++;
	this.notify_left_card(this.card_count);
    }

    this.get_move_func = function(src, dst) {
	if(src instanceof FreeSlot && dst instanceof FieldColumn) return this.free2field;
	if(src instanceof FieldColumn && dst instanceof FieldColumn) return this.field2field;
	if(src instanceof FieldColumn && dst instanceof FreeSlot) return this.field2free;
	if(dst instanceof HomeSlot) return this.move2home;
	if(src instanceof HomeSlot) return this.moveFromHome;
    }

    this.super_move_unpack = function(stack, old_src_col, new_src_col) {
	for( var i = 0 ; i < stack.length ; i++) {
	    var src, dst; [src, dst] = stack[i];
	    if(src instanceof FieldColumn && src == old_src_col) src = new_src_col;
	    this.get_move_func(dst, src).call(this, dst, src, SUPER_MOVE);
	}
    }

    this.super_move = function(src, dst, test) {
	if( src instanceof Card ) src = this.field_cols[src.pos_x];
	if( dst instanceof Card ) dst = this.field_cols[dst.pos_x];
	var empty_free = this.get_empty_free_slots();
	var empty_field = this.get_empty_cols(dst);
	var max = this.get_super_move_max(empty_free, empty_field);
	var series = new Array;
	var dst_card = dst.empty() ? null : dst.last();
	var src_cards = src.cards; 
	for(var row=src_cards.length-1; row>=0; row--) {
	    series.unshift(src_cards[row]);
	    if( row-1 < 0 ||
		!src_cards[row].fit_field(src_cards[row-1]) ||
		dst_card != null && src_cards[row].fit_field(dst_card) ||
		series.length == max ) {
		break;
	    }
	}
	var src_card = series.first();
	if(!src_card.fit_field(dst_card)) return false;
	if(test) return true;
	this.empty_select();
	var stack = new Array;
	this.record_step(SUPER_MOVE_BEGIN, null, null);
	this.super_move_pack(empty_free, empty_field, src, src_card, stack);
	if( stack.empty() ) {
	    this.steps.pop();
	    this.field2field(src, dst, NORMAL_MOVE);
	} else {
	    this.field2field(src, dst, SUPER_MOVE);
	    this.super_move_unpack(stack, src, dst);
	    this.record_step(SUPER_MOVE_END, null, null);
	}
	return true;
    }

    //only used in super move, undo
    this.field2free = function(src, dst, move_type) {
	return this.move2free(src, dst, true, move_type);//disable auto
    }

    //used in normal move and super, undo
    this.move2free = function(src, dst, disable_auto, move_type) {
	if(src.area == HOME) return false;
	if(src instanceof Card) {
	    if(src.area == FIELD) src=this.field_cols[src.pos_x];
	    if(src.area == FREE) src=this.free_slots[src.pos_x];
	}
	if(dst == null) {
	    dst = this.get_empty_free_slot();
	    if(dst == null ) return false;
	}
	var auto = false;
	var mt = move_type ? move_type : NORMAL_MOVE;
	if(src instanceof FieldColumn) {
	    //field => free
	    dst.set_card(src.last());
	    src.pop();
	    this.record_step(mt, src, dst);
	    auto = true;
	}
	if(src instanceof FreeSlot) {
	    //free => free
	    if( src != dst ) {
		dst.set_card(src.card);
		src.set_empty();
		this.record_step(mt, src, dst);
	    }
	}
	this.empty_select();
	if( auto && !disable_auto ) {
	    this.auto_play();
	}
	return true;
    }

    this.record_step = function(move_type, src, dst) {
	if(move_type != UNDO) this.steps.push([move_type, src, dst]);
    }

    //used in normal move and super move, undo
    this.free2field = function(src, dst, move_type, test) {
	if( src instanceof Card ) src = this.free_slots[src.pos_x];
	if( dst instanceof Card ) dst = this.field_cols[dst.pos_x];
	if( src.card.fit_field(dst.last()) || move_type == UNDO) {
	    if(test) return true;
	    dst.push(src.card);
	    src.set_empty();
	    this.empty_select();
	    var mt = move_type ? move_type : NORMAL_MOVE;
	    this.record_step(mt, src, dst);
	    return true;
	}
	return false;
    }

    this.move2field = function(src, dst, test) {
	if(src instanceof Card) {
	    if(src.area == FIELD) src=this.field_cols[src.pos_x];
	    if(src.area == FREE ) src=this.free_slots[src.pos_x];
	}
	if(dst instanceof Card) dst = this.field_cols[dst.pos_x];

	if(src instanceof FreeSlot) 
	    return this.free2field(src, dst, test) 
	if(src instanceof FieldColumn) {
	    if(src == dst) {
		this.empty_select();
		return true;//not alert user
	    }
	    //super move
	    if(this.super_move(src, dst, test) ) {
		if(!test)  this.auto_play();
		return true;
	    }
	    return false;
	}

    }
    
    //used in normal move and auto
    this.move2home = function(src, dst, auto_play, test) {
	if(src instanceof Card) {
	    if(src.area == FIELD) src=this.field_cols[src.pos_x];
	    if(src.area == FREE ) src=this.free_slots[src.pos_x];
	}
	if( dst instanceof Card) {
	    dst = this.home_slots[dst.pos_x];
	}
	var moved = false;
	var mt = auto_play ? AUTO_PLAY : NORMAL_MOVE;
	if(src instanceof FieldColumn) {
	    //field => home
	    if(src.last().fit_home(dst.card) ) {
		if(test) return true;
		dst.set_card(src.last());
		src.pop();
		this.record_step(mt, src, dst);
		moved = true;
	    }
	} 
	if(src instanceof FreeSlot) {
	    //free => home
	    if( src.card.fit_home(dst.card) ) {
		if(test) return true;
		dst.set_card(src.card);
		src.set_empty();
		this.record_step(mt, src, dst);
		moved = true;
	    }
	}
	if( moved ) {
	    this.card_count--;
	    this.notify_left_card(this.card_count);
	    return true;
	}
	return false;
    }

    this.is_safe_autoplay = function(card) {
	if( card.value() == ACE || card.value() == 1 )
	    return true; //A or 2
	var count = 0;
	
	for(var i=0;i<4;i++) {
	    var slot = this.home_slots[i];
	    if( !slot.empty() ) {
		if( (slot.card.is_back ^ card.is_black) && (slot.card.value() >= card.value()-1) ) {
		    count += 1;
		}
	    }
	}
	return ( count == 2 ) ? true : false;
    }

    this.auto_play = function() {
	trace('DeckCtrl::auto_play');
	//scan every last card in field col and free, check if fit home
	var auto = false;
	//scan every card in free slot, check if it fit home and safe to do auto play
	for( var i=0;i<4;i++ ) {
	    var src = this.free_slots[i];
	    if(src.empty()) continue;
	    for( var j=0;j<4;j++ ) {
		var dst = this.home_slots[j];
		if( src.card.fit_home(dst.card) && this.is_safe_autoplay(src.card) )
		{
		    this.move2home(src, dst, true);//auto play
		    auto = true;
		    break;
		}
	    }
	}
	//scan every last card in field column, check if it fit home and safe to auto play
	for( var i=0;i<8;i++ ) {
	    var src = this.field_cols[i];
	    if(src.empty()) continue; 
	    for( var j=0;j<4;j++ ) {
		var dst = this.home_slots[j]; 
		if( src.last().fit_home(dst.card) && this.is_safe_autoplay(src.last()) ) {
		    this.move2home(src, dst, true);//auto play
		    auto = true;
		    break;
		}
	    }
	}
	//keep call me until auto is false 
	if( auto ) { 
	    this.auto_play();
	}
    }

    //click cards, field, free, home
    this.click_cards = function(id) {
	trace('click cards');
	var card = this.pos_map[id];
	if( card.area == FREE ) {
	    //select card in free slot
	    //1. no select before, just highlight it
	    //2. selected card before
	    //   a. card in free ==> card in free, just switch highlight
	    //   b. card in field ==> card in free, just switch highlight
	    this.select(card);
	    return;
	}

	if( card.area == FIELD ) {
	    //select card in field
	    var dst_col = this.field_cols[card.pos_x]
	    //1. no select before, just highlight the column's last card
	    if( !this.has_select() ) {
		if( card != dst_col.last() ) {
		    card = dst_col.last(); 
		}
		this.select(card);
		return;
	    }
	    //2. selected before
	    //   a. card in free ==> card in field, if fit, move to it, or, do nothing
	    //   b. card in field ==> card in field
	    if(!this.move2field(this.sel_card, dst_col)) {
		alert_usr();
		return;
	    }
	}

	if( card.area == HOME ) {
	    if( !this.move2home(this.sel_card, card) ) {
		alert_usr();
		return;
	    }
	    this.empty_select();
	    this.auto_play();
	}
    }


    this.dblclick_cards = function(id) {
	trace('double click card');
	var card = this.pos_map[id];
	/*
	if( card.area == FIELD && 
	    card != this.field_cols[card.pos_x].last() ) {
	    trace('double click the card of field, not the last, nothing happens');
	    return;
	}
	*/
	if( !this.move2free(card, null) )
	{
	    trace('can not do any move');
	}
	return;
    }


    //click empty free slot
    this.click_free_slot = function(id) {
	var slot = this.free_slots[id];
	if(!slot.empty() ) {
	    //click card on free slot will trigger click event on free slot also
	    return;
	}
	if( !this.has_select() ) {
	    //alert('You select empty free slot');
	    return;
	}
	this.move2free(this.sel_card, slot);
    }

    //click empty home slot
    this.click_home_slot = function(id) {
	var slot = this.home_slots[id];
	if(!slot.empty()) {
	    //click card on home slot will trigger click event on home slot also
	    return;
	}
	if( !this.has_select() ) {
	    //alert('You select empty home slot');
	    return;
	}
	if( !this.move2home(this.sel_card, slot) ) {
	    alert_usr();
	    return;
	} 
	this.empty_select();
    }

    //click empty field column or blank field area under cards
    this.click_field_column = function(id) {
	trace('DeckCtrl::click_field_column, %d', id);
	//1.no selected card before, select blank field, nothing will happen
	//2.selected card in field or free before, select blank field, 
	//just like select last card in this column
	//3.select cards in field column, will trigger click_cards, then trigger this function. need filter before
	if(!this.has_select()) {
	    trace('select blank area, nothing will happen!');
	    return;
	}
	//selected home or another field
	if(this.sel_card.area == FIELD && this.sel_card.pos_x == id) {
	    trace('select one card in field, or selected one field card before, then select blank area in same field again');
	    return;
	}
	if(!this.move2field(this.sel_card, this.field_cols[id])) {
	    alert_usr();
	}
    }
    this.reset = function() {
	if( this.playing ) {
	    this.playing = false;
	    this.card_count = 52;
	    for( var i = 0; i < 4; i++ ) {
		this.free_slots[i].set_empty();
		this.home_slots[i].set_empty();
	    }
	    for( var i = 0 ; i < 8; i++ ) 
		this.field_cols[i].set_empty(); 
	    this.pos_map = new Array(52+4+4);
	    this.steps.remove_all();
	    this.matched.remove_all();
	}
    }
    
    this.start = function(deck) {
	trace('deck size is %d', deck.length);
	if(this.playing)  this.reset();
	this.playing = true; 
	var max_col = 8;
	for(var col = 0 ; col < max_col ; col++) {
	    var field_col = this.field_cols[col];
	    if(col < 4) { max_row = 7; } else { max_row = 6; }
	    for(var row = 0 ; row < max_row ; row++) {
		var card = new Card;
		card.id = deck[max_col*row + col];
		card.dom = $('<div class="card" id="' + card.id + '"></div>');
		card.normalize();
		field_col.push(card);
		this.pos_map[card.id]=card;
	    }
	}
	this.card_count = 52;
	this.notify_left_card(this.card_count);
    }

    this.undo = function() {
	trace('DeckCtrl::undo');
	while(!this.steps.empty()) {
	    var mt, src, dst, go=false; 
	    [mt,src,dst] = this.steps.last();
	    if(mt == AUTO_PLAY || mt == SUPER_MOVE) go = true;
	    if(mt == SUPER_MOVE_END) {
		this.steps.pop();
		continue;
	    }
	    if(mt == SUPER_MOVE_BEGIN) {
		this.steps.pop();
		break;
	    }
	    this.get_move_func(dst, src).call(this, dst, src, UNDO); 
	    this.steps.pop();
	    if(!go) break;//normal_move will break;
	}
    }
    this.redo = function() {
	trace('DeckCtrl::redo');
    }

    constructor();
};

var deck_ctrl = null;

function bind_events() {
    $('.card').click( function() {
	    var id = $(this).attr('id');
	    deck_ctrl.click_cards( parseInt(id) );
	});
    $('.card').dblclick( function() {
	    var id = $(this).attr('id');
	    deck_ctrl.dblclick_cards( parseInt(id) );
	});
    $('.freecell').click( function() {
	    var id = $(this).attr('id');
	    deck_ctrl.click_free_slot(parseInt(id));
	});
    $('.homecell').click( function() {
	    var id = $(this).attr('id');
	    deck_ctrl.click_home_slot(parseInt(id));
	});
    $('.fieldcolumn').click( function() {
	    var id = $(this).attr('id');
	    deck_ctrl.click_field_column(parseInt(id));
	});

    /*
    $('.freecell').hover( 
	function() {
	    trace('freecell hover in');
	    if(!deck_ctrl.sel_card) return;
	    var id = parseInt($(this).attr('id'));
	    if(deck_ctrl.free_slots[id].empty())
		$(this).append('<img src="/site_media/hand1.gif" />');
	},
	function() {
	    trace('freecell hover out');
	    $(this).find('img').remove();
	}
    );
    $('.homecell').hover( 
	function() {
	    trace('homecell hover it');
	    if(!deck_ctrl.sel_card) return;
	    var id = parseInt($(this).attr('id'));
	    if( jQuery.inArray(deck_ctrl.free_slots[id], deck_ctrl.matched) != -1 )
		$(this).append('<img src="/site_media/hand1.gif" />');
	},
	function() {
	    trace('homecell hover out');
	    $(this).find('img').remove();

	}
    );
    $('.card').hover( 
	function() {
	    trace('card hover it');
	    if(!deck_ctrl.sel_card) return;
	    var id = parseInt($(this).attr('id'));
	    var card = deck_ctrl.pos_map[id];
	    var dst = null;
	    if(card.area == FREE) return;
	    if(card.area == FIELD) { 
		dst = deck_ctrl.field_cols[card.pos_x];
		if(card != dst.last()) return; 
	    }
	    if(card.area == HOME) dst = deck_ctrl.home_slots[card.pos_x];
	    if(jQuery.inArray(dst, deck_ctrl.matched) != -1) {
		$(this).append('<img src="/site_media/hand1.gif" />');
	    }
	    
	},
	function() {
	    trace('card hover out');
	    $(this).find('img').remove();
	}
    );

    $('#flag_area').hover( 
	function() {
	    $(this).css('background-image', 'url(/site_media/flag2.gif)');
	},
	function() {
	    $(this).css('background-image', 'url(/site_media/flag1.gif)');
	}
    );
    */
}


$(function() {
	//place home cell, flag, freecell
	deck_ctrl = new DeckCtrl;
	deck_ctrl.init();
	deck_ctrl.notify_left_card = function(left_count) {
	    var obj = $("#status_area");
	    if( obj.is(":hidden") ) { 
		obj.show(); 
	    }
	    obj.html("left: " + left_count);
	}
	$("#action_undo").click(function(){
		deck_ctrl.undo();
	    });
	$("#action_redo").click(function(){
		deck_ctrl.redo();
	    });

	$("#action_play").click( function() {
		var MAX_SEED = 32000;
		var seed = Math.round( Math.random() * MAX_SEED);
		trace("the random seed is " + seed);
		seed = 2206;
		$.getJSON("/freecell/get_deck_by_seed", { "seed" : seed }, function(json) {
			trace("JSON data is " + json.deck[0]);
			deck_ctrl.start(json.deck);
			bind_events();
		    });
	    });
    });

